package com.tanhua.server.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.server.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
public class UserService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${tanhua.sso.url}")
    private String ssoUrl;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    //通过sso rest接口查询
    public User queryUserByToken(String token) {
        String url = ssoUrl + "/user/" + token; //对应sso层的userContoller中的queryUserByToken方法上方的路径/user/${token}
        try {
            String data = restTemplate.getForObject(url, String.class); //根据相应的参数类型String拿到响应的数据String data
            return MAPPER.readValue(data, User.class); //将拿到的JSON数据反序列化
        } catch (Exception e) {
            log.error("校验token出错! token=" + token, e);
        }
        return null;
    }
}
